package org.dreamwork.tools.simulator.connector.managed;

import org.dreamwork.db.SQLite;
import org.dreamwork.tools.simulator.connector.data.RequestLog;

import javax.annotation.Resource;
import java.util.List;

@Resource
public class RequestLogService {
    @Resource
    private SQLite sqlite;

    public void save (RequestLog log) {
        sqlite.save (log, false);
    }

    public List<RequestLog> findRequestLogs (String interfaceId, int pageNo, int pageSize) {
        String sql = "SELECT * FROM request_log WHERE if_id = ? ORDER BY request_time DESC LIMIT ? OFFSET ?";
        if (pageSize < 0) pageSize = 30;
        int offset = (pageNo - 1) * pageSize;
        if (offset < 0) offset = 0;

        return sqlite.list (RequestLog.class, sql, interfaceId, pageSize, offset);
    }
}